import { defineStore } from 'pinia'

export interface UserInfo {
    id: number
    username: string
    mobile: string
    last_time: string
    last_ip: string
    token: string
    refreshToken: string,
    money:string,
    score:string,
    avatar:string,
    label_id:string,
    isSetPwd:boolean,
    email:string

}

export const useUserInfo = defineStore('userInfo', {
    state: (): UserInfo => {
        return {
            id: 0,
            username: '',
            mobile: '',
            last_time: '',
            last_ip: '',
            token: '',
            refreshToken: '',
            money: '',
            score: '',
            avatar:'',
            label_id:'',
            isSetPwd:false,
            email:''
        }
    },
    actions: {
        removeToken() {
            this.token = ''
            this.refreshToken = ''
        },
        dataFill(state: UserInfo) {
            this.$state = { ...this.$state, ...state }
        },
        setToken(token: string, type: 'auth' | 'refresh') {
            const field = type == 'auth' ? 'token' : 'refreshToken'
            this[field] = token
        },
        getToken(type: 'auth' | 'refresh' = 'auth') {
            return type === 'auth' ? this.token : this.refreshToken
        },
        logout() {
           
        },
        isLogin() {
            return this.id && this.token
        },
    },
    persist: {
        key: 'userInfo',
    },
})